/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.node.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jzwl.common.constant.AjaxStatusConstant;
import com.jzwl.site.manager.node.service.SiteNodeService;
import com.jzwl.site.manager.nodetype.service.SiteNodeTypeService;
import com.jzwl.system.base.controller.BaseController;

/**
 * SiteNodeType
 * SiteNodeType
 * siteNodeType
 * <p>Title:SiteNodeTypeController </p>
 * 	<p>Description:SiteNodeType </p>
 * 	<p>Company: </p> 
 * 	@author aotu-code 
 */
@Controller
@RequestMapping("/siteNode")
public class SiteNodeController  extends BaseController {
	
	@Autowired
	private SiteNodeService siteNodeService;
	
	@Autowired
	private SiteNodeTypeService siteNodeTypeService;
	
	/**
	 * 跳转到添加SiteNodeType的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddSiteNode")
	public ModelAndView toAddsiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		
		List<Map<String, Object>> types = siteNodeTypeService.querySiteNodeTypeAll();
		
		mov.addObject("nodetypes", types);
		mov.addObject("parentId", paramsMap.get("pid"));
		mov.setViewName("/manager/node/add");
		return mov;
	}
	
	/**
	 * 添加SiteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addSiteNode")
	public ModelAndView addsiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		
		try{
			boolean flag = siteNodeService.addSiteNode(paramsMap);
			if(!flag){
				mov.addObject("msg", "添加SiteNode失败!");
				mov.setViewName("/error");
				
			}else{
				mov.setViewName("/manager/node/sucess");
			}
		}catch(Exception e){
			e.printStackTrace();
			mov.addObject("msg", "添加SiteNode失败!");
			mov.setViewName("/error");
		}
		
		
		return mov;
	}
	
	
	/**
	 * 跳转到添加SiteNodeType的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toUpdateSiteNode")
	public ModelAndView toUpdateSiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		
		Map nodeinfo = siteNodeService.queryNodeInfoById(paramsMap);
		List<Map<String, Object>> types = siteNodeTypeService.querySiteNodeTypeAll();
		mov.addObject("node", nodeinfo);
		mov.addObject("nodetypes", types);
		
		mov.addObject("level", paramsMap.get("level"));
		
		mov.setViewName("/manager/node/edit");
		return mov;
	}
	
	/**
	 * 添加SiteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateSiteNode")
	public ModelAndView updateSiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		
		try{
			boolean flag = siteNodeService.updateSiteNode(paramsMap);
			if(!flag){
				mov.addObject("msg", "更新SiteNode失败!");
				mov.setViewName("/error");
				
			}else{
				mov.setViewName("/manager/node/sucess");
			}
		}catch(Exception e){
			e.printStackTrace();
			mov.addObject("msg", "更新SiteNode失败!");
			mov.setViewName("/error");
		}
		
		return mov;
	}
	
	
	@RequestMapping(value = "/delSiteNode")
	public ModelAndView delSiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		
		try{
			boolean flag = siteNodeService.delSiteNode(paramsMap);
			if(!flag){
				mov.addObject("msg", "更新SiteNode失败!");
				mov.setViewName("/error");
				
			}else{
				mov.setViewName("/manager/node/sucess");
			}
		}catch(Exception e){
			e.printStackTrace();
			mov.addObject("msg", "更新SiteNode失败!");
			mov.setViewName("/error");
		}
		
		return mov;
	}
	
	/**
	 */
	@RequestMapping(value = "/siteNodeList")
	public ModelAndView siteNodeList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		mov.setViewName("/manager/node/nodeindex");
		return mov;
	}
	
	@RequestMapping(value = "/topNodeTree")
	public @ResponseBody Map<String, Object> topNodeTree(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
		createParameterMap(request);
		try{
			List<Map<String, Object>> topList = siteNodeService.queryNodeInfo(paramsMap);
			resultMap.put("list", topList);
		}catch(Exception e){
			e.printStackTrace();
			resultMap.put("error", "获取栏目数异常");
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/defaultjsp")
	public ModelAndView defaultjsp(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		mov.setViewName("/manager/node/new");
		return mov;
	}
	
}

